package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import dao.StudiengangDao;
import dao.KursDao;

public class AllStudgang extends AbstractController {
	
	private StudiengangDao studiengangDao;
	private KursDao kursDao;

	public KursDao getKursDao() {
		return kursDao;
	}

	public void setKursDao(KursDao kursDao) {
		this.kursDao = kursDao;
	}

	public void setStudiengangDao(StudiengangDao studiengangDao) {
		this.studiengangDao = studiengangDao;
	}
	
	public StudiengangDao getStudiengangDao() {
		return studiengangDao;
	}
	
	protected ModelAndView handleRequestInternal(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse) throws Exception {
		
		ModelAndView mv = new ModelAndView();
		base_classes.Studiengang stdg = new base_classes.Studiengang();
		int idStudiengang = 0;
		try {
			idStudiengang = Integer.parseInt(httpServletRequest.getParameter("studiengang"));
			
		} catch(NumberFormatException e)
		{
			
		}
		
		if(idStudiengang!= 0) {
			stdg = studiengangDao.findById(idStudiengang);
			mv.addObject("studgangID", stdg.getIdStudiengang());
			mv.addObject("kurse", kursDao.findByStudiengang(studiengangDao.findById(idStudiengang)));
		}
		else {
			mv.addObject("kurse", kursDao.findAll());
		}
		mv.addObject("title", "Liste der Studiengaenge");
		mv.addObject("message", "Alle Studiengaenge der Faktultaet BWL/WI");
		mv.addObject("studiengaenge", studiengangDao.findAll());
		mv.setViewName("list-studiengaenge");
		return mv;
		
	}

}
